package com.wdcloud.niceandroid.netutil;

import android.os.Build;

import com.wdcloud.framework_business.utils.LogUtil;
import com.wdcloud.netlibrary.NetHelper;
import com.wdcloud.netlibrary.callback.NetWorkCallback;
import com.wdcloud.netlibrary.request.IRequestBuild;
import com.wdcloud.niceandroid.BuildConfig;

import java.util.HashMap;
import java.util.Map;

import static com.wdcloud.niceandroid.utils.GsonUtil.getGsonInstance;


/**
 * Info:
 * Created by Umbrella.
 * CreateTime: 2021/3/8 10:57
 */
public class MyNetUtil {
    public static final String SERVER_URL_OFFLINE_HOST = BuildConfig.BASE_URL;// 线下
    public static final String SERVER_URL_ONLINE_HOST = "";// 线上
    public static final String H5_BASE_URL = BuildConfig.H5_BASE_URL;

    /**
     * get 请求
     *
     * @param uri
     * @param params
     * @param callback
     */
    public static void getData(String uri, Map<String, String> params, NetWorkCallback<BaseBean> callback) {
        HashMap<String, String> map = getBaseParams();
        if (null != params && !params.isEmpty()) {
            map.putAll(params);
        }
        if (!uri.contains("http"))
            uri = SERVER_URL_OFFLINE_HOST + uri;
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + uri + "\n" + "request_data=" + map);
        InitAndAddHeaders(RequestType.doGet)
                .url(uri).params(map)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    public static void postNiceJson(String uri, Object object, NetWorkCallback<BaseBean> callback) {
        String paramJson = getGsonInstance().toJson(object);
        if (!uri.contains("http"))
            uri = SERVER_URL_OFFLINE_HOST + uri;
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + uri + "\n" + "request_data=" + paramJson);
        InitAndAddHeaders(RequestType.doJsonPost)
                .json(paramJson)
                .url(uri)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    public static void postData(String uri, Map<String, String> params, NetWorkCallback<BaseBean> callback) {
        HashMap<String, String> map = getBaseParams();
        if (null != params && !params.isEmpty()) {
            map.putAll(params);
        }
        if (uri.contains("timingNotify")) {
            map.remove("userId");
        }
        String json = getGsonInstance().toJson(map);//JSON.toJSONString(map);//map转json字符串
        if (!uri.contains("http"))
            uri = SERVER_URL_OFFLINE_HOST + uri;
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + uri + "\n" + "request_data=" + json);
        InitAndAddHeaders(RequestType.doJsonPost)
                .json(json)
                .url(uri)
                .params(map)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    public static void doGet(String path, Map<String, String> params, NetWorkCallback callback) {
        HashMap<String, String> map = getBaseParams();
        if (null != params && !params.isEmpty()) {
            map.putAll(params);
        }
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + SERVER_URL_OFFLINE_HOST + path + "\n" + "request_data=" + map);
        InitAndAddHeaders(RequestType.doGet)
                .url(SERVER_URL_OFFLINE_HOST + path)
                .params(map)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    public static void doPost(String path, Map<String, String> params, NetWorkCallback callback) {
        /**获取公共参数*/
        HashMap<String, String> map = getBaseParams();
        if (null != params && !params.isEmpty()) {
            map.putAll(params);
        }
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + SERVER_URL_OFFLINE_HOST + path + "\n" + "request_data=" + map);
        InitAndAddHeaders(RequestType.doPost)
                .url(SERVER_URL_OFFLINE_HOST + path)
                .params(map)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    public static void doPostJson(String path, Map<String, String> params, NetWorkCallback callback) {
        /**获取公共参数*/
        HashMap<String, String> map = getBaseParams();
        if (null != params && !params.isEmpty()) {
            map.putAll(params);
        }
        String json = getGsonInstance().toJson(map);//map转json字符串
        if (BuildConfig.IS_DEBUG)
            LogUtil.i("request_url=" + SERVER_URL_OFFLINE_HOST + path + "\n" + "request data===============" + json);
        InitAndAddHeaders(RequestType.doJsonPost)
                .url(SERVER_URL_OFFLINE_HOST + path)
                .json(json)
                .buildEvent().backOnMain(true).enqueue(callback);
    }

    /**
     * 初始化网络请求 并 添加公共headers
     *
     * @param requestType 请求类型
     * @return IRequestBuild 实例
     */
    private static IRequestBuild InitAndAddHeaders(RequestType requestType) {
        IRequestBuild requestBuild;
        switch (requestType) {
            case doGet:
                requestBuild = NetHelper.getInstance().doGet();
                break;
            case doPost:
                requestBuild = NetHelper.getInstance().doPost();
                break;
            case doJsonPost:
                requestBuild = NetHelper.getInstance().doJsonPost();
                break;
            default:
                requestBuild = NetHelper.getInstance().doGet();
        }
        requestBuild
                .addHeader("origin", BuildConfig.BASE_URL)
//                .addHeader("token", TextUtils.isEmpty(getToken()) ? "" : getToken())
//                .addHeader("appVersion", MyApplication.getInstance().getVersionName())
                .addHeader("platform", "Android")
                .addHeader("osVersion", Build.VERSION.RELEASE);
        return requestBuild;
    }

    /**
     * 获取公共参数
     *
     * @return
     */
    private static HashMap<String, String> getBaseParams() {
        HashMap<String, String> baseMap = new HashMap<String, String>();
        /**机构列表机构分支*/
//        baseMap.put("branch", SPDataHelper.getInstance().getString(SPKeyConstants.SP_KEY_BRANCH));
        /**机构列表机构关联id*/
//        baseMap.put("userId", SPDataHelper.getInstance().getString(SPKeyConstants.SP_KEY_ORGAN_USER_ID));
        /**
         * 用户信息id
         * 与后台接口人员确认考虑到安全问题，参数不允许带用户id的，后端都是透传
         * 接口里都不需要
         * */
//        int id = SPDataHelper.getInstance().getInt(SPKeyConstants.SP_KEY_USER_ID);
//        if(id != 0){
//            baseMap.put("id", id + "");
//        }
        baseMap.put("platform", "Android");
        return baseMap;
    }

    /**
     * 请求类型枚举
     */
    private enum RequestType {
        doJsonPost,
        doPost,
        doGet
    }
}